package views;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeMap;

import data.ScheduleActivityPlacer;

import entities.ScheduleActivity;
import entities.ScheduleDay;

import android.content.Context;
import android.view.Display;
import android.widget.RelativeLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**
 * Get and scheduleDay object and draws the objects in it
 * 
 * @author Christian
 * 
 */
public class ScheduleDayDrawer extends RelativeLayout {

	private ArrayList<ScheduleActivity> act;
	private ScheduleActivityPlacer sap = new ScheduleActivityPlacer();

	public ScheduleDayDrawer(Context context, ScheduleDay sDay, int width) {
		super(context);
		this.act = sDay.getActivites();
		
		if(sap.isTimeCollision(act)){
			
			try {
				sap.scheduleFixer(act);
			} catch (Exception e) {
				System.out.println("Something wrong with scheduleFixer");
			}
		}

		// create new for all the activities in day
		for (int i = 0; i < this.act.size(); i++) {

			// new table row
			TableRow t1 = new TableRow(context);

			// changes background color so you can view it, should be specific
			t1.setBackgroundColor(-16777216);
			if (this.act.get(i).getTimeShare() == 1) {
				// new params
				LayoutParams params = new LayoutParams(width, timeDiffMin(act
						.get(i).getStartTime(), act.get(i).getEndTime()));

				params.setMargins(0,
						(int) (timeDiffMin(800, act.get(i).getStartTime())), 0,
						0);

				// sets the params
				t1.setLayoutParams(params);

			} else {
				// new params
				LayoutParams params = new LayoutParams(width
						/ this.act.get(i).getTimeShare(), timeDiffMin(act
						.get(i).getStartTime(), act.get(i).getEndTime()));

				if (this.act.get(i).getTimeSharePos() == 2) {
					params.setMargins(
							width / 2,
							(int) (timeDiffMin(800, act.get(i).getStartTime())),
							0, 0);
				} else {
					params.setMargins(
							0,
							(int) (timeDiffMin(800, act.get(i).getStartTime())),
							0, 0);
				}
				// sets the params
				t1.setLayoutParams(params);
			}
			// the height of the schedule activity start -> end * the pixel
			// height for each minute
			t1.setMinimumHeight((int) (timeDiffMin(act.get(i).getStartTime(),
					act.get(i).getEndTime())));

			// new text view and sets the text
			TextView text1 = new TextView(context);
			text1.setText(Integer.toString(act.get(i).getStartTime()) + " - "
					+ act.get(i).getEndTime() + " " + act.get(i).getSubject()
					+ " in " + act.get(i).getRoom() + " with "
					+ act.get(i).getTeacher());
			t1.addView(text1);
			this.addView(t1);
		}

	}

	// returns the difference between start and end in min
	private int timeDiffMin(int start, int end) {
		int res = 0;

		res = ((end / 100) * 60) + (end % 100);
		res -= ((start / 100) * 60) + (start % 100);

		return res;
	}


}
	
